@extends('layout.master')

@section('content-header')
    <h1>Kabupaten</h1>
    <ol class="breadcrumb">
        <li><a href="/"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        <li class="active"><i class="fa fa-cloud"></i> Kabupaten</li>
    </ol>
@stop

@section('content')
<div class="row">
    <div class="col-xs-12">

        <div class="box">
            <div class="box-body table-responsive">
                {{ HTML::decode(link_to_route('kabupatens.create', '<i class="fa fa-plus-square"></i> Tambah Data', null, array('class'=>'btn btn-primary'))) }}
                <table id="my-table" class="table table-bordered table-striped">
                    <thead>
                        <tr class="center">
                            <th>#</th>
                            <th>Provinsi</th>
                            <th>Kabupaten</th>
                            <th>Keterangan</th>
                            <th width="10%">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div><!-- /.box-body -->
        </div><!-- /.box -->
    </div>
</div>
@stop

@section('javascript')
<script type="text/javascript">
    var data;
    $(function() {
       data = $('#my-table').dataTable({
            "aoColumns": [
                { "bSearchable": false, "bSortable": false, "sClass": "text-center" },        
                { "bSearchable": true, "bSortable": true },        
                { "bSearchable": true, "bSortable": true },    
                { "bSearchable": false, "bSortable": false, "sClass": "text-center", "bVisible": false },     
                { "bSearchable": false, "bSortable": false, "sClass": "text-center" }     
            ],
            "iDisplayLength": 10,
            "bProcessing": true,
            "bServerSide": true,
            "bLengthChange": false,
            "sAjaxSource": "{{ URL::route('kabupatens.data-table') }}",
            "bPaginate": true,
            "bSort": true,
            "bAutoWidth": false,
            "fnServerData": function( sSource, aoData, fnCallback ){                           
                $.getJSON( sSource, aoData, function (json) {
                    fnCallback(json);
                });
            }
        });

        $(data).bind('draw', function(event) {
            var i = 1;
            var oSettings = data.fnSettings();
            page = Math.ceil(oSettings._iDisplayStart/oSettings._iDisplayLength);
            $('tbody tr').each(function(index, el) {
                $(el).find('td:eq(0)').html((page*10)+i);
                i++;
            });
        });

        $('.table-responsive').on('click', '.btn-delete', function(event) {
            event.preventDefault();
            formDelete = $(this).data('form');
            bootbox.confirm('Yakin hapus data ini?'+formDelete, function(yes){
                if(yes) {
                    $('.formDelete').submit();
                }
            });
        });  

        $('.table-responsive').on('click', '.btn-detail', function(event) {
            event.preventDefault();
            var detail = '<table class="table"><tr><td width="10%">Provinsi</td><td width="2%">:</td><td>'+$(this).data('provinsi')+'</td></tr>';
            detail += '<tr><td>Kabupaten</td><td>:</td><td>'+$(this).data('nama')+'</td></tr>';
            detail += '<tr><td>Keterangan</td><td>:</td><td>'+$(this).data('keterangan')+'</td></tr></table>';
            bootbox.dialog({
                message: detail,
                title: 'Detail Kabupaten',
                buttons: {
                    main: {
                        label: "OK",
                        className: "btn-primary"
                    }
                }
            });
        });
    });
</script>
@stop